package com.sdyd.antivirus.service.impl;

import com.sdyd.antivirus.dao.UserInfoMapper;
import com.sdyd.antivirus.model.UserInfo;
import com.sdyd.antivirus.model.UserInfoKey;
import com.sdyd.antivirus.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo selectByPrimaryKey(UserInfoKey user_infoKey1) {
        return userInfoMapper.selectByPrimaryKey(user_infoKey1);
    }

    @Override
    public String insertUserInfo(UserInfo user_info) {


        //先查询当天是否存在数据，如果存在，进行更新，如果不存在，进行插入
        UserInfoKey user_infoKey = new UserInfoKey();
        //获取当天时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String curTime = formatter.format(date);

        user_infoKey.setAddTime(curTime);
        user_infoKey.setPhoneNumber(user_info.getPhoneNumber());
        user_infoKey.setUserName(user_info.getUserName());
        user_infoKey.setGroupId(user_info.getGroupId());
        UserInfo user_info_selected = userInfoMapper.selectByPrimaryKey(user_infoKey);
        user_info.setAddTime(curTime);
        if(user_info_selected == null){
            System.out.println("插入数据");
            userInfoMapper.insert(user_info);
        } else{
            System.out.println("更新数据");
            userInfoMapper.updateByPrimaryKey(user_info);
        }

        return null;
    }

    @Override
    public Map getGroupInfo(String groupId) {
        Map groupInfo = userInfoMapper.getGroupInfo(groupId);
        return groupInfo;
    }
}
